// indexviews/doctorView/doctorView.js
var servicePath = require("../../utils/servicePath.js");
var globalData = getApp().globalData;
var util = require('../../utils/util.js');
var alert = require('../../utils/alert.js');
var app = getApp(); 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    doctor: null,
    judges: [],
    appDomain: app.globalData.appDomain,
    doctorId:null,
    isBook:"false"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var doctorId = options.id;
    this.setData({
      doctorId:doctorId
    })
    this.isBook(options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //判断是否关注
  isBook: function (id) {
    var userid = globalData.backUserInfo.syS0100;
    var _this = this;
    var url = getApp().globalData.apiDomain + "/WAttention?userid=" + userid + "&doctorId=" + id;
    wx.request({
      url: url,
      method: "GET",
      success: function (res) {
        _this.setData({
          isBook: res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var doctorId = this.data.doctorId;
    if(doctorId != null) {
      var _this = this;
      wx.request({
        url: servicePath.SingleDoctorService() + "?doctorId=" + doctorId,
        method: "GET",
        success: function (res) {
          //医生信息
          _this.setData({
            doctor: res.data.condition
          })
          console.log(res.data.condition)
        }
      })
    }
   
  },

//取消关注
  cancelFocuse: function (e) {
    var userId = getApp().globalData.backUserInfo.syS0100;
    var toId = e.currentTarget.dataset.toid;
    var url = getApp().globalData.apiDomain + "/WAttention";
    var _this = this;
    wx.request({
      url: url,
      method: "DELETE",
      data: {
        userID: userId,
        doctorID: toId
      },
      success: function (res) {
        console.log("**************取消关注**********")
        console.log(res);
        if (res.data.code == 200) {
          wx.showModal({
            title: '提示',
            content: '取消关注成功！',
          })
          _this.setData({
            isBook:"false"
          })
        }
      }
    })
  },
  //添加关注
  addFocuse: function (e) {
    var userId = getApp().globalData.backUserInfo.syS0100;
    var toId = e.currentTarget.dataset.toid;
    var _this = this;
    var url = getApp().globalData.apiDomain + "/WAttention";
    wx.request({
      url: url ,
      method:"POST",
      data:{
        userID:userId,
        doctorID:toId
      },
      success:function(res){
        console.log("***************加关注**********")
        console.log(res);
        if(res.data.code == 200) {
          wx.showModal({
            title: '提示',
            content: '关注成功！',
          })
          _this.setData({
            isBook:"true"
          })
        }
        if(res.data.code == 300) {
          wx.showModal({
            title: '提示',
            content: '您已经关注该医生',

          })
          _this.setData({
            isBool: "true"
          })
        }
      }
    })
   
  }
})